package com.proton.carepatchtemp.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.MsgAdapter;
import com.proton.carepatchtemp.base.BaseFragment;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.daomanager.MsgManager;
import com.proton.carepatchtemp.database.table.Msg;
import com.proton.carepatchtemp.database.table.Report;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.MsgCenter;
import com.sdsmdg.tastytoast.TastyToast;
import com.sinping.iosdialog.animation.BaseAnimatorSet;
import com.sinping.iosdialog.animation.BounceEnter.BounceTopEnter;
import com.sinping.iosdialog.animation.SlideExit.SlideBottomExit;
import com.sinping.iosdialog.dialog.widget.NormalDialog;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SystemMsgCenterFragment extends BaseFragment {
    public boolean isCheckAll = false;
    Set<Report> deleteReports = new HashSet<>();
    private RecyclerView recyclerView;
    private MsgAdapter mMsgAdapter;
    private List<Msg> msgList = new ArrayList<>();
    private BaseAnimatorSet bas_in;
    private BaseAnimatorSet bas_out;
    /**
     * 暂时没有任何消息哦！
     */
    private TextView mTvBitmapSysMsg;
    private RecyclerView mRcvSysMsgList;
    /**
     * 删除
     */
    private TextView mTvDeleteAllChecked;
    /**
     * 全选
     */
    private TextView mTvSelectAll;
    private LinearLayout mLlyMenu;

    public SystemMsgCenterFragment() {
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SystemMsgCenterFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SystemMsgCenterFragment newInstance(String param1, String param2) {
        SystemMsgCenterFragment fragment = new SystemMsgCenterFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.NET_MSG_GETLIST:
                if (null == mMsgAdapter || null == mRcvSysMsgList) {
                    return;
                }
                msgList.clear();
                msgList.addAll(MsgManager.getAllMsg());
                if (msgList.size() <= 0) {
                    mTvBitmapSysMsg.setVisibility(View.VISIBLE);
                } else {
                    mMsgAdapter.notifyDataSetChanged();
                    mTvBitmapSysMsg.setVisibility(View.GONE);
                }
                break;
            case Codes.MSG_BULK_DELETION:
                deleteReports.clear();
                mLlyMenu.animate()
                        .alpha(1.0f)
                        .translationYBy(-mLlyMenu.getHeight())
                        .setDuration(100).start();
                mRcvSysMsgList.setPadding(0, 0, 0, mLlyMenu.getHeight());
                mMsgAdapter.isBulkDeletion = true;
                mMsgAdapter.notifyDataSetChanged();
                break;
            case Codes.MSG_CANCEL_BULK_DELETION:
                mMsgAdapter.isBulkDeletion = false;
                for (Msg msg :
                        mMsgAdapter.mDelMsgList) {
                    msg.setChecked(false);
                }
                mMsgAdapter.mDeleteList.clear();
                mMsgAdapter.mDelMsgList.clear();
                mLlyMenu.animate()
                        .alpha(0f)
                        .translationYBy(mLlyMenu.getHeight())
                        .setDuration(100).start();
                mRcvSysMsgList.setPadding(0, 0, 0, 0);
                mMsgAdapter.notifyDataSetChanged();
                break;
            case Codes.NET_MSG_DELETE:
                if (event.isSuccess()) {
                    TastyUtils.makeTextShort(R.string.string_delete_select_msg_success, TastyToast.SUCCESS);
                    mMsgAdapter.getDatas().removeAll(mMsgAdapter.mDelMsgList);
                    mMsgAdapter.mDeleteList.clear();
                    mMsgAdapter.mDelMsgList.clear();
                    mMsgAdapter.notifyDataSetChanged();
                    if (0 >= msgList.size()) {
                        MsgCenter.msggetlist(1, Extras.DVC_TYPE_TMP);
                    }
                } else {
                    TastyUtils.makeTextShort(R.string.string_delete_select_msg_fail, TastyToast.ERROR);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        msgList.addAll(MsgManager.getAllMsg());
        if (null != msgList || msgList.size() > 0) {
            //LogUtils.e(msgList.size());
        }

        bas_in = new BounceTopEnter();
        bas_out = new SlideBottomExit();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mLlyMenu == null) return;
            mLlyMenu.postDelayed(() -> {
                MsgCenter.msggetlist(1, Extras.DVC_TYPE_TMP);
                //批量删除重新显示后清除
                isCheckAll = false;
                mMsgAdapter.isBulkDeletion = false;
                for (Msg msg : msgList) {
                    msg.setChecked(false);
                }
                mMsgAdapter.mDelMsgList.clear();
                mMsgAdapter.mDeleteList.clear();
                mMsgAdapter.notifyDataSetChanged();
                if (mLlyMenu.getAlpha() != 0f) {
                    mLlyMenu.animate()
                            .alpha(0f)
                            .translationYBy(mLlyMenu.getHeight())
                            .setDuration(100).start();
                    mRcvSysMsgList.setPadding(0, 0, 0, 0);
                }
            }, 500);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_system_msg_center, container, false);
        recyclerView = rootView.findViewById(R.id.rcv_sys_msg_list);
        mMsgAdapter = new MsgAdapter(msgList, getContext());
        LinearLayoutManager linearLayoutManagerayout = new LinearLayoutManager(getContext());
        linearLayoutManagerayout.setStackFromEnd(true);
        linearLayoutManagerayout.setReverseLayout(true);
        recyclerView.setLayoutManager(linearLayoutManagerayout);
        recyclerView.setAdapter(mMsgAdapter);
        recyclerView.setVisibility(View.VISIBLE);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        initView(rootView);
        setListener();
        if (msgList.size() <= 0) {
            mTvBitmapSysMsg.setVisibility(View.VISIBLE);
        } else {
            mTvBitmapSysMsg.setVisibility(View.GONE);
        }
        return rootView;
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }


    private void initView(View rootView) {
        mTvBitmapSysMsg = rootView.findViewById(R.id.tv_bitmap_sys_msg);
        mRcvSysMsgList = rootView.findViewById(R.id.rcv_sys_msg_list);
        mTvDeleteAllChecked = rootView.findViewById(R.id.tv_delete_all_checked);
        mTvSelectAll = rootView.findViewById(R.id.tv_select_all);
        mLlyMenu = rootView.findViewById(R.id.lly_menu);
    }

    private void setListener() {
        mTvSelectAll.setOnClickListener(v -> {
            if (isCheckAll) {
                isCheckAll = false;
                for (Msg msg :
                        msgList) {
                    msg.setChecked(false);
                }
                mMsgAdapter.mDelMsgList.clear();
                mMsgAdapter.mDeleteList.clear();
            } else {
                isCheckAll = true;
                mMsgAdapter.mDelMsgList.clear();
                mMsgAdapter.mDelMsgList.addAll(msgList);
                for (Msg msg :
                        msgList) {
                    mMsgAdapter.mDeleteList.add(msg.getId());
                    msg.setChecked(true);
                }
            }
            mMsgAdapter.notifyDataSetChanged();
        });

        mTvDeleteAllChecked.setOnClickListener(v -> {
            if (mMsgAdapter.mDeleteList.size() <= 0) {
                TastyUtils.makeTextShort(R.string.string_have_no_chose_msg, TastyToast.WARNING);
                return;
            }
            deleteCheckedMsgs();
        });
    }


    private void deleteCheckedMsgs() {
        final NormalDialog dialog = new NormalDialog(getContext());
        dialog.content(getString(R.string.string_confirm_delete_msg))//
                .style(NormalDialog.STYLE_TWO)//
                .titleTextSize(23)//
                .showAnim(bas_in)//
                .dismissAnim(bas_out)//
                .show();

        dialog.setOnBtnClickL(
                dialog::dismiss,
                () -> {
                    MsgCenter.deleteSome(mMsgAdapter.mDeleteList);
                    dialog.dismiss();
                });

    }
}
